import config from 'config';
import {IStorage} from "./objectStorage/type";

export type envConfig = {
  userId: string
  appId: string
  nodeId: string
}

export type GatewayConfig = {
  endpoint: string
}

export type Configuration = {
  objectStorage?: IStorage
  gatewayConfig?: GatewayConfig
  envConfig?: envConfig
};

class ConfigurationService {
  
  private _config: Configuration;

  public constructor() {
    this._config = this.loadConfiguration();
  }

  public get config(): Configuration {
    return this._config;
  }

  public loadConfiguration(): Configuration {
    const result = {} as Configuration;

    result.objectStorage = {
      type: config.has('objectStorage.type') ? config.get('objectStorage.type') : 'minio',
      config: {
        host: config.has('objectStorage.config.host') ? config.get('objectStorage.config.host') : 'localhost',
        port: config.has('objectStorage.config.port') ? config.get('objectStorage.config.port') : 7000,
        bucket: config.has('objectStorage.config.bucket') ? config.get('objectStorage.config.bucket') : 'suanpan',
        useSSL: config.has('objectStorage.config.useSSL') ? config.get('objectStorage.config.useSSL') : false,
        accessKey: config.has('objectStorage.config.accessKey') ? config.get('objectStorage.config.accessKey') : 'admin',
        accessSecret: config.has('objectStorage.config.accessSecret') ? config.get('objectStorage.config.accessSecret') : 'admin123456'
      }
    };

    result.gatewayConfig = {
      endpoint: config.has('gateway.endpoint') ? config.get('gateway.endpoint') : 'http://sim-gateway.default:7777',
    }

    result.envConfig = {
      userId: config.has('env.userId') ? config.get('env.userId') : '1',
      appId: config.has('env.appId') ? config.get('env.appId') : '1',
      nodeId: config.has('env.nodeId') ? config.get('env.nodeId') : '1',
    }

    return result;
  }

  public getKey(key: string): any {
    return this._config[key];
  }
}

export const configurationService = new ConfigurationService();